﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using WarehouseWebAPP.code.BLL;
using WarehouseWebAPP.code.DAL;

namespace WarehouseWebAPP.Pages.Auth
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        private bool IsAdministrator(Guid UserType)
        {
            UserTypeBLL userTypeBLL = new UserTypeBLL();
            Guid UserTypeNum = userTypeBLL.UserTypeGetByName("Administrator")[0].user_type_num;
            if (UserTypeNum == UserType) return true;

            return false;

        }

        private string[] getRole(string UserName)
        {
            
            string[] role = new string[1];
            if (UserName.Equals("god"))
            {
                role[0] = "admin";
                return role;
            }
            try
            {
                UserBLL userBLL = new UserBLL();
                if (userBLL.userGetByLogin(UserName).Count < 1)
                    return null;
                UserDS.userRow loginUserRow = userBLL.userGetByLogin(UserName)[0];
                if (IsAdministrator(loginUserRow.user_type))
                    role[0] = "admin";
                else
                    role[0] = "user";
                return role;
            }
            //TODO dispose here
            finally
            {

            }
            
        }


        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.User != null)
            {
                // see if this user is authenticated, any authenticated cookie (ticket) exists for this user
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    // see if the authentication is done using FormsAuthentication
                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        // Get the roles stored for this request from the ticket
                        // get the identity of the user
                        FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
                        // get the forms authetication ticket of the user
                        FormsAuthenticationTicket ticket = identity.Ticket;
                        // get the roles stored as UserData into the ticket
                        string[] roles =  getRole(ticket.Name);
                        HttpContext.Current.User = 
                            new System.Security.Principal.GenericPrincipal(identity, roles);
                    }
                }
            }
        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}